﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace Qpm.Automap.Core.UI
{
  public partial class TasksPage : Wizard.UI.InternalWizardPage
  {
    public TasksPage()
    {
      InitializeComponent();
      this.Banner.Title = "Step 3 of 4 - Select Tasks";
      this.Banner.Subtitle = "Select tasks to be included in report";
      dgv_Tasks.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dgv_Tasks_DataBindingComplete);
    }

    private void TasksPage_SetActive(object sender, CancelEventArgs e)
    {
      this.Anchor = AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom;
      this.Size = this.Parent.Size;
      SetWizardButtons(Wizard.UI.WizardButtons.Back | Wizard.UI.WizardButtons.Next);
      if (dgv_Tasks.DataSource == null)
      {

        parameters.tasks = ProjectReader.GetProjectTasks(parameters.tempProjectFile);

        SelectTasksInConfig(parameters.tasks);

        dgv_Tasks.DataSource = parameters.tasks;

        dgv_Tasks.Columns[0].Width = 40;

        for (int i = 1; i < dgv_Tasks.Columns.Count; i++)
        {
          dgv_Tasks.Columns[i].ReadOnly = true;
          if (i > 3)
          {
            dgv_Tasks.Columns[i].Visible = false;
          }
        }
        dgv_Tasks.Columns[1].Width = 40;
        dgv_Tasks.Columns[1].HeaderText = "";
        dgv_Tasks.Columns[3].Width = 300;

        comboBox1.SelectedIndex = 2;
      }
    }

    private void SelectTasksInConfig(DataTable taskTable)
    {
      if (!File.Exists(parameters.configFile))
        return;

      XmlDocument xdoc = new XmlDocument();
      xdoc.Load(parameters.configFile);

      XmlNodeList taskNodes = xdoc.SelectNodes("//SelectedTasks/TaskUID");
      foreach (XmlElement taskElement in taskNodes)
      {
        int taskUid = taskElement.InnerText == null || taskElement.InnerText.Trim().Length == 0 ? -1 : Convert.ToInt32(taskElement.InnerText);
        if (taskUid < 0)
          continue;
        DataRow[] taskRows = taskTable.Select("UID = " + taskUid.ToString());
        foreach (DataRow taskRow in taskRows)
        {
          taskRow["Select"] = true;
        }
      }
    }

    private void button4_Click(object sender, EventArgs e)
    {
      DataTable table = dgv_Tasks.DataSource as DataTable;
      string filterString = table.DefaultView.RowFilter;
      foreach (DataRow row in table.Select(filterString))
      {
        row["Select"] = true;
      }
    }

    private void button3_Click(object sender, EventArgs e)
    {
      string filterString = "OutlineLevel ";
      switch (comboBox1.SelectedIndex)
      {
        case 0: filterString += "=";
          break;
        case 1: filterString += ">";
          break;
        case 2: filterString += ">=";
          break;
        case 3: filterString += "<";
          break;
        case 4: filterString += "<=";
          break;
      }
      filterString += numericUpDown1.Value.ToString();
      DataTable table = dgv_Tasks.DataSource as DataTable;
      table.DefaultView.RowFilter = filterString;
    }

    private void button6_Click(object sender, EventArgs e)
    {
      DataTable table = dgv_Tasks.DataSource as DataTable;
      string filterString = table.DefaultView.RowFilter;
      foreach (DataRow row in table.Select(filterString))
      {
        row["Select"] = false;
      }
    }

    private void styleTaskGrid()
    {
      foreach (DataGridViewRow taskRow in dgv_Tasks.Rows)
      {
        DataRowView taskView = taskRow.DataBoundItem as DataRowView;
        DataRow task = taskView.Row;
        Font summaryCellFont = new Font(dgv_Tasks.Font, FontStyle.Bold);
        DataGridViewCellStyle style = dgv_Tasks.DefaultCellStyle.Clone();
        style.Font = summaryCellFont;
        if ((bool)task["IsSummary"])
        {
          taskRow.Cells[3].Style = style;
        }
      }
    }

    void dgv_Tasks_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
      styleTaskGrid();
    }
  }
}
